{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## This notebook demonstrates the usage of the Fuzzy Inference System pipeline. It trains a FIS on the gas consumption data and shows the forecasting performance on a small test batch. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import csv\n",
    "import os\n",
    "import sys\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import time\n",
    "\n",
    "# add path of needed code \n",
    "parent_directory = os.path.abspath('..')\n",
    "sys.path.append(parent_directory+'/Code/')\n",
    "\n",
    "from Fuzzification import cluster, scale\n",
    "import Reader\n",
    "import FIS\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The header: \n",
      "['gas', 'before1', 'before2', 'peak5', 'sum5', 'peak24', 'sum24', 'mean15', 'hour', 'FH', 'T', 'Q', 'U', 'peak5T', 'diffT', 'std_day', 'std_year', 'kwh', 'kwhpeak5', 'day_year', 'day_week', 'next_day_week']\n"
     ]
    }
   ],
   "source": [
    "path = parent_directory +'/Data/gas/'\n",
    "file = 'train1'\n",
    "data = Reader.read_gas_data(path, file)\n",
    "# for this data set, the target column is de first column\n",
    "target_col = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# specify overlap of your sets, or the variance of the gaussian\n",
    "overlap = 0.07\n",
    "# Gaussian, triangle or trapezoid\n",
    "mf = 'Gaussian'\n",
    "# hand picked number of clusters per feature\n",
    "number_of_centroids = [11,7,7,7,9,9,11,9,11,11,11,11,11,9,11,9,9,9,9,11,7,7]\n",
    "# A name for the FIS file\n",
    "FIS_name='Demo_WM_gas'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Training (large data set, takes a while)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The training took:  42.095319986343384 seconds\n"
     ]
    }
   ],
   "source": [
    "start = time.time()\n",
    "FIS.train(FIS_name, data, target_col, mf, number_of_centroids, overlap)\n",
    "print('The training took: ', time.time() - start, 'seconds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# read in the trained FIS\n",
    "method, mf, overlap, target_centroids, feature_centroids, RB = FIS.read(FIS_name +'.FIS')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The header: \n",
      "['gas', 'before1', 'before2', 'peak5', 'sum5', 'peak24', 'sum24', 'mean15', 'hour', 'FH', 'T', 'Q', 'U', 'peak5T', 'diffT', 'std_day', 'std_year', 'kwh', 'kwhpeak5', 'day_year', 'day_week', 'next_day_week']\n"
     ]
    }
   ],
   "source": [
    "path = parent_directory +'/Data/gas/'\n",
    "file = 'test1'\n",
    "data = Reader.read_gas_data(path, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The testing took:  20.733866930007935 seconds\n"
     ]
    }
   ],
   "source": [
    "# testing of the FIS (NB takes a lot of time so reduce data size)\n",
    "DATA_SIZE = 100\n",
    "start = time.time()\n",
    "RMSE, MAE, _ = FIS.test(data[0:DATA_SIZE], mf, overlap, target_centroids, feature_centroids, RB, target_col)\n",
    "print('The testing took: ', time.time() - start, 'seconds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The RMSE is:  237.84805824\n",
      "The MAE is:  11.8220940461\n",
      "The number of rules is  40906\n"
     ]
    }
   ],
   "source": [
    "print('The RMSE is: ', RMSE)\n",
    "print('The MAE is: ' , MAE)\n",
    "print('The number of rules is ', len(RB))"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
